﻿using Knight.Common.Security;

using Knight.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Knlght.Domain;
using Knight.Interface;

namespace WebApp.Areas.Manage.Controllers
{
    public abstract class BaseController : Controller
    {
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        protected string Encipher(string password)
        {
            return DES.DesEncrypt(password, DConfig.DESKEY);
        }

        #region ActionJson
        /// <summary>
        /// 返回操作Json结果
        /// </summary>
        /// <param name="success">操作是否成功</param>
        /// <param name="message">返回的消息</param>
        /// <param name="code">错误编号</param>
        /// <param name="behavior">是否允许客户端的Http Get请求</param>
        /// <returns>{Success:{0},Message:{1},Code:{2}}</returns>
        protected internal JsonResult EJActionJson(bool success, string message, string code, JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet)
        {
            return Json(new
            {
                Success = success,
                Message = message,
                Code = code
            }, behavior);
        }
        /// <summary>
        /// 返回操作Json结果
        /// </summary>
        /// <param name="message">返回的消息</param>
        /// <returns>{success:{0},message:{1}}</returns>
        protected internal JsonResult EJActionJson(bool success = true, string message = "", JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet)
        {
            return Json(new
            {
                Success = success,
                Message = message
            }, behavior);
        }
        /// <summary>
        /// 返回操作Json结果
        /// </summary>
        protected internal JsonResult EJActionJson(ActionResponse rsp, JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet)
        {
            return Json(new
            {
                Success = rsp.IsSuccess,
                Message = rsp.Message,
                Code = rsp.ResultCode
            }, behavior);
        }
        /// <summary>
        /// 返回操作Json结果
        /// </summary>
        protected internal JsonResult EJActionWithIdJson(ActionResponse rsp, JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet)
        {
            if (rsp.IsSuccess)
            {
                return Json(new
                {
                    Success = true,
                    Id = rsp.Message
                }, behavior);
            }
            else
            {
                return Json(new
                {
                    Success = false,
                    Message = rsp.Message,
                    Code = rsp.ResultCode
                }, behavior);
            }
        }
        /// <summary>
        /// 返回分页Json结果
        /// </summary>
        protected internal JsonResult EJPagingJson<T>(PagingResponse<T> rsp, JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet) where T : class
        {
            if (rsp.IsSuccess)
            {
                return Json(new
                {
                    Success = rsp.IsSuccess,
                    Message = "",
                    TotalPages = rsp.TotalPages,
                    TotalModels = rsp.TotalModels,
                    Models = rsp.Models
                }, behavior);

            }
            else
            {
                return Json(new
                {
                    Success = rsp.IsSuccess,
                    Message = rsp.Message,
                    Code = rsp.ResultCode
                }, behavior);
            }
        }
        /// <summary>
        /// 返回单一返回对象的Json结果
        /// </summary>
        /// <returns>{success:true,data:{0}}|{Success:false,Message:{0}}</returns>
        protected internal JsonResult EJItemJson<T>(ItemResponse<T> rsp, JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet)
        {
            if (rsp.IsSuccess)
            {
                return Json(new
                {
                    Success = true,
                    Model = rsp.Model
                });
            }
            else
            {
                return Json(new
                {
                    Success = false,
                    Code = rsp.ResultCode,
                    Message = rsp.Message
                });
            }
        }
        /// <summary>
        /// 返回对象集合的Json结果
        /// </summary>
        /// <returns>{success:true,datas:{0}}|{success:false,message:{0}}</returns>
        protected internal JsonResult EJListJson<T>(ListResponse<T> rsp, JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet)
        {
            if (rsp.IsSuccess)
            {
                return Json(new
                {
                    Success = true,
                    Models = rsp.Models
                });
            }
            else
            {
                return Json(new
                {
                    Success = false,
                    Code = rsp.ResultCode,
                    Message = rsp.Message
                });
            }
        }
        #endregion

        #region 请求值传换辅助方法
        /// <summary>
        /// 转换成bool值(true,on,1,yes)
        /// </summary>
        protected bool? GetParamToBoolean(string paramName, bool? defaultValue = null)
        {
            var value = Request.Params[paramName];
            if (!string.IsNullOrEmpty(value))
            {
                var lowerValue = value.ToLower();
                return lowerValue == "true" || lowerValue == "on" || lowerValue == "1" || lowerValue == "yes";
            }
            return defaultValue;
        }
        protected string GetParamToString(string paramName, string defaultValue = null)
        {
            var value = Request.Params[paramName];
            if (value == null)
                return defaultValue;
            else
                return value;
        }
        protected int? GetParamToInt32(string paramName, int? defaultValue = null)
        {
            var value = Request.Params[paramName];
            if (!string.IsNullOrEmpty(value))
            {
                int result = 0;
                if (int.TryParse(value, out result))
                {
                    return result;
                }
            }
            return defaultValue;
        }
        protected long? GetParamToInt64(string paramName, long? defaultValue = null)
        {
            var value = Request.Params[paramName];
            if (!string.IsNullOrEmpty(value))
            {
                long result = 0;
                if (long.TryParse(value, out result))
                {
                    return result;
                }
            }
            return defaultValue;
        }
        protected double? GetParamToDouble(string paramName, double? defaultValue = null)
        {
            var value = Request.Params[paramName];
            if (!string.IsNullOrEmpty(value))
            {
                double result = 0;
                if (double.TryParse(value, out result))
                {
                    return result;
                }
            }
            return defaultValue;
        }
        protected decimal? GetParamToDouble(string paramName, decimal? defaultValue = null)
        {
            var value = Request.Params[paramName];
            if (!string.IsNullOrEmpty(value))
            {
                decimal result = 0;
                if (decimal.TryParse(value, out result))
                {
                    return result;
                }
            }
            return defaultValue;
        }
        protected DateTime? GetParamToDateTime(string paramName, DateTime? defaultValue = null)
        {
            var value = Request.Params[paramName];
            if (!string.IsNullOrEmpty(value))
            {
                DateTime result = DateTime.MinValue;
                if (DateTime.TryParse(value, out result))
                {
                    return result;
                }
            }
            return defaultValue;
        }
        protected TEnum? GetParamToEnum<TEnum>(string paramName, TEnum? defaultValue = null) where TEnum : struct
        {
            var value = Request.Params[paramName];
            if (!string.IsNullOrEmpty(value))
            {
                TEnum result;
                if (Enum.TryParse(value, out result))
                {
                    return result;
                }
            }
            return defaultValue;
        }
        #endregion


        /// <summary>
        /// 获取指定的服务接口对象
        /// </summary>
        protected T getService<T>() where T : class
        {
            return Knight.ObjectFactory.ObjectFactory.Get<T>();
        }

        protected IDefaultService getDefaultService()
        {
            return Knight.ObjectFactory.ObjectFactory.Get<IDefaultService>();
        }

        public ActionResult ErrorView(string error)
        {
            return View();
        }
    }
}